package com.zhangsheng.imserver.task;

import com.alibaba.fastjson.JSON;
import com.zhangsheng.contant.IMRabbit;
import com.zhangsheng.enums.IMCmdType;
import com.zhangsheng.imserver.netty.IMServerGroup;
import com.zhangsheng.imserver.netty.processor.AbstractMessageProcessor;
import com.zhangsheng.imserver.netty.processor.ProcessorFactory;
import com.zhangsheng.model.IMRecvInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author 张晟
 * @version 1.0
 * @date 2024/3/28 14:33
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class PullMessageTask {

    private final IMServerGroup serverGroup;
    @RabbitListener(queues = {IMRabbit.IM_PRIVATE_MESSAGE_QUEUE, IMRabbit.IM_GROUP_MESSAGE_QUEUE})
    public void pullMessage(String message){
        if (!serverGroup.isReady()){
            throw new RuntimeException("server未启动");
        }
        IMRecvInfo recvInfo = JSON.parseObject(message, IMRecvInfo.class);
        AbstractMessageProcessor processor = ProcessorFactory.createProcessor(IMCmdType.fromCode(recvInfo.getCmd()));
        processor.process(recvInfo);
    }

}
